package com.gao.gLotusMusic.view;

import com.jfoenix.controls.JFXSlider;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;



public class BottomUI extends HBox {
    public static BottomUI instance = new BottomUI();

    private BottomUI() {
        initUI();
        initListener();
    }

    private final ImageView prev = new ImageView(new Image("pic/prev.png"));
    private final ImageView play = new ImageView(new Image("pic/play.png"));
    private final ImageView next = new ImageView(new Image("pic/next.png"));


    private final Label playedTime = new Label("00:00");
    private final Label totalTime = new Label("00:00");

    private final JFXSlider progress = new JFXSlider(0, 100, 0);
    private final JFXSlider volume = new JFXSlider(0, 100, 0);
    private final Button playerListBtn = new Button("播放列表");
    private PlayerList playerList;


    private void initUI() {
        setAlignment(Pos.CENTER_LEFT);
        getChildren().add(prev);
        getChildren().add(play);
        getChildren().add(next);
        getChildren().add(playedTime);
        getChildren().add(progress);
        getChildren().add(totalTime);
        getChildren().add(volume);
        getChildren().add(playerListBtn);
        HBox.setHgrow(progress, Priority.ALWAYS);
        playerList = PlayerList.instance;
    }

    private void initListener() {
        prev.onMouseClickedProperty().addListener((observableValue, eventHandler, t1) -> System.out.println("prev"));
        play.onMouseClickedProperty().addListener((observableValue, eventHandler, t1) -> System.out.println("play"));
        next.onMouseClickedProperty().addListener((observableValue, eventHandler, t1) -> System.out.println("next"));
        progress.valueProperty().addListener((observableValue, number, t1) -> {
            int n = (int) t1.longValue();
            System.out.println(n);
            playedTime.setText(n + "");
        });
        volume.valueProperty().addListener((observableValue, number, t1) -> System.out.println(t1));
        playerListBtn.setOnAction(e -> {
            playerList.show(Main.getPrimaryStage());
            double screenX = playerListBtn.getScene().getWindow().getX()
                    + playerListBtn.getScene().getX() + playerListBtn.localToScene(0, 0).getX()
                    + playerListBtn.getBoundsInParent().getWidth() - playerList.getWidth();
            double screenY = playerListBtn.getScene().getWindow().getY()
                    + playerListBtn.getScene().getY() + playerListBtn.localToScene(0, 0).getY()
                    - playerList.getHeight();
            playerList.setX(screenX);
            playerList.setY(screenY);
        });
    }

}
